package net.zcscloud.zhuohcun.zeco.service;

import net.zcscloud.zhuohcun.zeco.common.service.LogicService;
import net.zcscloud.zhuohcun.zeco.dao.UserDao;
import net.zcscloud.zhuohcun.zeco.common.JwtUtil;
import net.zcscloud.zhuohcun.zeco.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;



@Service
public class UserService extends LogicService<UserDao,User,Long> {
    @Autowired
    private UserDao userDao;

    public UserService(UserDao lr) {
        super(lr);
    }

    public String login(User userDto) {
        User user = userDao.findByName(userDto.getName());
        if (user == null) {
            throw new RuntimeException("用户名或密码错误");
        }
        if (!user.getPassword().equals(userDto.getPassword())) {
            throw new RuntimeException("用户名或密码错误");
        }
        return JwtUtil.generateToken(user.getName(),43200000);
    }
    public String verifyToken(String token) {
        try {
            if (JwtUtil.validateToken(token)) {
                return "0"; //有效
            }
            return "1"; //过期
        } catch (Exception e) {
            return "2"; //无效
        }
    }
}